package com.newtouch.bxzs.database.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.common.base.model.OrganizationMap;
import com.newtouch.bxzs.database.model.OrganizationMapBO;
import com.newtouch.bxzs.database.model.OrganizationMapByUserBO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *映射关系Mapper类
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020/12/22 14:49</td>
 * <td>lhx</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lhx
 * @version 1.0.0
 * @since 1.0.0
 */
@Mapper
public interface OrganizationMappingMapper extends BaseMapper<OrganizationMap> {

    @Select(value = "select org.ORGANIZATION_NAME ,opposite.COOPERATION_COMPANY_ORGANIZATION_NAME,mapping.*,mapping.IS_ENABLE as enableFlag \n" +
            "from ASSISTANT_ORGANIZATION org,ASSISTANT_COOPERATION_COMPANY_ORGANIZATION opposite,ASSISTANT_ORGANIZATION_MAP mapping \n" +
            "where mapping.ORGANIZATION_ID = org.ORGANIZATION_ID and mapping.COOPERATION_COMPANY_ORGANIZATION_ID = opposite.COOPERATION_COMPANY_ORGANIZATION_ID and mapping.IS_DELETE = '0' order by mapping.CREATE_TIME desc")
    Page<OrganizationMapBO> organizationMappingBO(Page<OrganizationMapBO> page);

    @Select(value = "select *\n" +
            "from(\n" +
            "select org.ORGANIZATION_NAME ,opposite.COOPERATION_COMPANY_ORGANIZATION_NAME,mapping.*,mapping.IS_ENABLE as enableFlag\n" +
            "from ASSISTANT_ORGANIZATION org,ASSISTANT_COOPERATION_COMPANY_ORGANIZATION opposite,ASSISTANT_ORGANIZATION_MAP mapping \n" +
            "where mapping.ORGANIZATION_ID = org.ORGANIZATION_ID and mapping.COOPERATION_COMPANY_ORGANIZATION_ID = opposite.COOPERATION_COMPANY_ORGANIZATION_ID and mapping.IS_DELETE = '0' order by mapping.CREATE_TIME desc \n" +
            ") u WHERE u.ORGANIZATION_NAME like (#{organizationName})\n" +
            "or u.COOPERATION_COMPANY_ORGANIZATION_NAME like concat(#{organizationName})")
    Page<OrganizationMapBO> organizationMappingCondition(@Param("organizationName") String organizationName, Page<OrganizationMapBO> page);

    @Select(value = "select org.ORGANIZATION_NAME ,opposite.COOPERATION_COMPANY_ORGANIZATION_CODE,opposite.COOPERATION_COMPANY_ORGANIZATION_NAME,mapping.* \n" +
            "from ASSISTANT_ORGANIZATION org,ASSISTANT_COOPERATION_COMPANY_ORGANIZATION opposite,ASSISTANT_ORGANIZATION_MAP mapping \n" +
            "where mapping.ORGANIZATION_ID = org.ORGANIZATION_ID and mapping.COOPERATION_COMPANY_ORGANIZATION_ID = opposite.COOPERATION_COMPANY_ORGANIZATION_ID and mapping.IS_DELETE = '0' and mapping.ORGANIZATION_MAP_ID = #{orgMappID} ")
    OrganizationMapBO organizationMappingInfo(@Param("orgMappID") String orgMappID);

    @Select({"<script>select * from(select org.ORGANIZATION_NAME,org.ORGANIZATION_CODE,org.ORGANIZATION_NAME_FULL_SPELLING,opposite.COOPERATION_COMPANY_ORGANIZATION_NAME,opposite.COOPERATION_COMPANY_ORGANIZATION_NAME_FULL_SPELLING," +
            "opposite.COOPERATION_COMPANY_ORGANIZATION_CODE,mapping.*,mapping.IS_ENABLE as enableFlag\n" +
            " from ASSISTANT_ORGANIZATION org,ASSISTANT_COOPERATION_COMPANY_ORGANIZATION opposite,ASSISTANT_ORGANIZATION_MAP mapping \n" +
            " where mapping.ORGANIZATION_ID = org.ORGANIZATION_ID and mapping.COOPERATION_COMPANY_ORGANIZATION_ID = opposite.COOPERATION_COMPANY_ORGANIZATION_ID and mapping.IS_DELETE = '0' and mapping.IS_ENABLE = '1' " +
            " order by opposite.COOPERATION_COMPANY_ORGANIZATION_NAME_FULL_SPELLING)u " +
            "WHERE u.ORGANIZATION_CODE in " +
            " <foreach collection='userOrganizationCode' index='index' item='item' open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item}" +
            "</foreach>" +
            "<if test='dataSource != null and dataSource != \"\"'>\n" +
            "and  u.DATA_SOURCE = #{dataSource}" +
            " </if>" +
            "<if test='organizationName != null and organizationName != \"\"'>\n" +
            "and  u.COOPERATION_COMPANY_ORGANIZATION_NAME like concat('%',#{organizationName},'%')" +
            "</if></script>"})
    List<OrganizationMapByUserBO> organizationMappingByCode(@Param("userOrganizationCode") List<String> userOrganizationCode,
                                                            @Param("dataSource") String dataSource,
                                                            @Param("organizationName") String organizationName);

}
